package com.irdstudio.efp.nls.service.impl.queue;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.efp.nls.service.dao.NlsQueueTaskDao;
import com.irdstudio.efp.nls.service.domain.queue.NlsQueueTask;
import com.irdstudio.efp.nls.service.facade.queue.NlsQueueTaskService;
import com.irdstudio.efp.nls.service.vo.queue.NlsQueueTaskVO;

/**
 * 网贷任务队列
 * 
 * @author huangwb
 * @since 2018年12月27日 上午8:33:47
 * @version 1.0
 */
@Service("nlsQueueTaskService")
public class NlsQueueTaskServiceImpl implements NlsQueueTaskService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(NlsQueueTaskServiceImpl.class);

	@Autowired
	private NlsQueueTaskDao nlsQueueTaskDao;

	/**
	 * 新增操作
	 * 
	 * @throws Exception
	 */
	@Override
	public int insertNlsQueueTask(NlsQueueTaskVO inNlsQueueTaskVo) throws Exception {
		logger.debug("当前新增数据为:" + inNlsQueueTaskVo.toString());
		int num = 0;
		try {
			NlsQueueTask nlsQueueTask = new NlsQueueTask();
			beanCopy(inNlsQueueTaskVo, nlsQueueTask);
			num = nlsQueueTaskDao.insertNlsQueueTask(nlsQueueTask);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
			if (e instanceof DuplicateKeyException) {
				throw new Exception("新增数据发生异常，主键冲突!");
			}
			throw e;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(NlsQueueTaskVO inNlsQueueTaskVo) {
		logger.debug("当前删除数据条件为:" + inNlsQueueTaskVo);
		int num = 0;
		try {
			NlsQueueTask nlsQueueTask = new NlsQueueTask();
			beanCopy(inNlsQueueTaskVo, nlsQueueTask);
			num = nlsQueueTaskDao.deleteByPk(nlsQueueTask);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsQueueTaskVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(NlsQueueTaskVO inNlsQueueTaskVo) {
		logger.debug("当前修改数据为:" + inNlsQueueTaskVo.toString());
		int num = 0;
		try {
			NlsQueueTask nlsQueueTask = new NlsQueueTask();
			beanCopy(inNlsQueueTaskVo, nlsQueueTask);
			num = nlsQueueTaskDao.updateByPk(nlsQueueTask);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inNlsQueueTaskVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public NlsQueueTaskVO queryByPk(NlsQueueTaskVO inNlsQueueTaskVo) {

		logger.debug("当前查询参数信息为:" + inNlsQueueTaskVo);
		try {
			NlsQueueTask queryNlsQueueTask = new NlsQueueTask();
			beanCopy(inNlsQueueTaskVo, queryNlsQueueTask);
			NlsQueueTask queryRslNlsQueueTask = nlsQueueTaskDao.queryByPk(queryNlsQueueTask);
			if (Objects.nonNull(queryRslNlsQueueTask)) {
				NlsQueueTaskVO outNlsQueueTaskVo = beanCopy(queryRslNlsQueueTask, new NlsQueueTaskVO());
				logger.debug("当前查询结果为:" + outNlsQueueTaskVo.toString());
				return outNlsQueueTaskVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsQueueTaskVO> queryAllOwner(NlsQueueTaskVO nlsQueueTaskVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<NlsQueueTaskVO> list = null;
		try {
			List<NlsQueueTask> nlsQueueTasks = nlsQueueTaskDao.queryAllOwnerByPage(nlsQueueTaskVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + nlsQueueTasks.size());
			pageSet(nlsQueueTasks, nlsQueueTaskVo);
			list = (List<NlsQueueTaskVO>) beansCopy(nlsQueueTasks, NlsQueueTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsQueueTaskVO> queryAllCurrOrg(NlsQueueTaskVO nlsQueueTaskVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<NlsQueueTask> nlsQueueTasks = nlsQueueTaskDao.queryAllCurrOrgByPage(nlsQueueTaskVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + nlsQueueTasks.size());
		List<NlsQueueTaskVO> list = null;
		try {
			pageSet(nlsQueueTasks, nlsQueueTaskVo);
			list = (List<NlsQueueTaskVO>) beansCopy(nlsQueueTasks, NlsQueueTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsQueueTaskVO> queryAllCurrDownOrg(NlsQueueTaskVO nlsQueueTaskVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<NlsQueueTask> nlsQueueTasks = nlsQueueTaskDao.queryAllCurrDownOrgByPage(nlsQueueTaskVo);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + nlsQueueTasks.size());
		List<NlsQueueTaskVO> list = null;
		try {
			pageSet(nlsQueueTasks, nlsQueueTaskVo);
			list = (List<NlsQueueTaskVO>) beansCopy(nlsQueueTasks, NlsQueueTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询待执行的队列任务表
	 * 
	 * @return
	 * @author huangwb
	 * @since 2018年12月27日 下午7:09:18
	 * @version 1.0
	 */
	@Override
	public List<String> queryExecutoryQueueTask(String queueTaskState) {
		logger.debug("当前查询待执行的队列任务表信息的参数信息为:");
		List<String> applySeqList = nlsQueueTaskDao.queryExecutoryQueueTask(queueTaskState);
		logger.debug("当前查询待执行的队列任务表信息的结果集数量为:" + applySeqList.size());

		return applySeqList;
	}

	/**
	 * 根据申请流水号和待执行状态查询队列任务
	 * 
	 * @param nlsQueueTaskVO
	 * @return
	 * @author huangwb
	 * @since 2018年12月27日 下午9:54:01
	 * @version 1.0
	 */
	@Override
	public NlsQueueTaskVO queryLockedExecutoryQueueTask(NlsQueueTaskVO nlsQueueTaskVO) {
		logger.debug("当前查询参数信息为:" + nlsQueueTaskVO);
		try {
			NlsQueueTask queryNlsQueueTask = new NlsQueueTask();
			beanCopy(nlsQueueTaskVO, queryNlsQueueTask);
			NlsQueueTask queryRslNlsQueueTask = nlsQueueTaskDao.queryLockedExecutoryQueueTask(queryNlsQueueTask);
			if (Objects.nonNull(queryRslNlsQueueTask)) {
				NlsQueueTaskVO outNlsQueueTaskVo = beanCopy(queryRslNlsQueueTask, new NlsQueueTaskVO());
				logger.debug("当前查询结果为:" + outNlsQueueTaskVo.toString());
				return outNlsQueueTaskVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询挂起的队列任务表
	 * 
	 * @param queueTaskState
	 * @return
	 * @author huangwb
	 * @since 2019年1月2日 下午9:00:47
	 * @version 1.0
	 */
	@Override
	public List<String> queryHangQueueTask(String queueTaskState) {
		logger.debug("当前查询挂起的队列任务表信息的参数信息为:");
		List<String> applySeqList = nlsQueueTaskDao.queryHangQueueTask(queueTaskState);
		logger.debug("当前查询挂起的队列任务表信息的结果集数量为:" + applySeqList.size());

		return applySeqList;
	}

	/**
	 * 根据申请流水号和挂起状态查询队列任务
	 * 
	 * @param nlsQueueTaskVO
	 * @return
	 * @author huangwb
	 * @since 2019年1月2日 下午9:00:47
	 * @version 1.0
	 */
	@Override
	public NlsQueueTaskVO queryLockedHangQueueTask(NlsQueueTaskVO nlsQueueTaskVO) {
		logger.debug("当前查询参数信息为:" + nlsQueueTaskVO);
		try {
			NlsQueueTask queryNlsQueueTask = new NlsQueueTask();
			beanCopy(nlsQueueTaskVO, queryNlsQueueTask);
			NlsQueueTask queryRslNlsQueueTask = nlsQueueTaskDao.queryLockedHangQueueTask(queryNlsQueueTask);
			if (Objects.nonNull(queryRslNlsQueueTask)) {
				NlsQueueTaskVO outNlsQueueTaskVo = beanCopy(queryRslNlsQueueTask, new NlsQueueTaskVO());
				logger.debug("当前查询结果为:" + outNlsQueueTaskVo.toString());
				return outNlsQueueTaskVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 分页查询列表数据
	 * 
	 * @param nlsQueueTaskVo
	 * @return
	 * @author qiuyf
	 * @since 2019年1月10日 下午5:49:14
	 * @version 1.0
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<NlsQueueTaskVO> queryListByCondition(NlsQueueTaskVO nlsQueueTaskVo) {
		logger.debug("根据查询条件分页查询列表数据开始...........");
		List<NlsQueueTaskVO> list = null;
		try {
			List<NlsQueueTask> nlsQueueTasks = nlsQueueTaskDao.queryListByConditionByPage(nlsQueueTaskVo);
			pageSet(nlsQueueTasks, nlsQueueTaskVo);
			list = (List<NlsQueueTaskVO>) beansCopy(nlsQueueTasks, NlsQueueTaskVO.class);
			logger.debug("根据查询条件分页查询列表数据结束..........");
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;
	}
	@Override
	@SuppressWarnings("unchecked")
	public List<NlsQueueTaskVO> queryAllCurrOwnerPrd(NlsQueueTaskVO nlsQueueTaskVo) {

		logger.debug("当前查询本人所属产品以下数据信息的参数信息为:");
		List<NlsQueueTask> nlsQueueTasks = nlsQueueTaskDao.queryAllCurrOwnerPrdByPage(nlsQueueTaskVo);
		logger.debug("当前查询本人所属产品及下数据信息的结果集数量为:" + nlsQueueTasks.size());
		List<NlsQueueTaskVO> list = null;
		try {
			pageSet(nlsQueueTasks, nlsQueueTaskVo);
			list = (List<NlsQueueTaskVO>) beansCopy(nlsQueueTasks, NlsQueueTaskVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}
}
